﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;

public partial class Galery : System.Web.UI.Page
{
    string[] PictureList = new string[0]; //[fotoFile.Length];
    int MapesNr = 0;
    internal class CustomCompare : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            return DateTime.Compare(
              Directory.GetCreationTime(y), Directory.GetCreationTime(x));
        }
    }
    DataRow[] Galrow;

    protected void Page_Load(object sender, EventArgs e)
    {
        //string virtualPathDir = Server.MapPath("~/galerijas/"); // atrod virtual path lapas Bāzes MAPI
        //string[] dirrAray = Directory.GetDirectories(virtualPathDir); // nolasa visas servera Mapes
        //Array.Sort(dirrAray, new CustomCompare()); // Sortēšana
        //string[] dirrArray2 = new string[dirrAray.Length];
        //int cipars =0; // Sagrupē mapes tā lai jaunākā ir pēdējā !!
        //for (int i = dirrAray.Length; i > 0; i--)
        //{
        //    dirrArray2[cipars] = dirrAray[i - 1];
        //    cipars++;
        //}
        MapesNr = Convert.ToInt16(String.Format(Request.QueryString["value"]));
        //string[] fotoFile = Directory.GetFiles(dirrArray2[MapesNr]);
        //virtualPathDir = Server.MapPath("~/");
        //int charCount = virtualPathDir.LastIndexOf("\\") + 1;

        //int PicturCount = 0;
        ////btn_count = fotoFile.Length;
        ////string textID = "Lbutton";
        //for (int i = 0; i < fotoFile.Length; i++)
        //{
        //    string EsošaisTeksts = fotoFile[i].Substring(charCount);
        //    string[] TextGabali = EsošaisTeksts.Split('\\');
        //    bool smallimg = false;
        //    smallimg = TextGabali[2].Contains("Small");
        //    if (smallimg == true)
        //    {
        //        PicturCount++;
        //        Array.Resize(ref PictureList, PicturCount);
        //        string jaunaisLinks = "";
        //        for (int i2 = 0; i2 < TextGabali.Length; i2++)
        //        {
        //            if (i2 == TextGabali.Length - 1)
        //            {
        //                jaunaisLinks += TextGabali[i2];
        //            }
        //            else { jaunaisLinks += TextGabali[i2] + "/"; }
        //        }
        //        PictureList[PicturCount - 1] = jaunaisLinks;
        //    }
        //}

        string virtualPathDir2 = Server.MapPath("~/");
        DataTable galery = new DataTable("galery");
        galery.ReadXml(virtualPathDir2 + "galery.xml");
        //DataRow[] Galrow;
        Galrow = galery.Select("MapID=" + MapesNr.ToString()); // Filtrs.
        int a = 0;
        foreach (DataRow Dr in Galrow)
        {
            if ((int)Dr[1] == MapesNr)
            {
                Array.Resize(ref PictureList, a + 1);
                PictureList[a] = (string)Dr[5];
                a++;
            }
        }

        if (Page.IsPostBack == false)
        {
        Repeater1.DataSource = PictureList;
        Repeater1.DataBind();
        }
    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int nummurs = e.Item.ItemIndex;
        string[] TextGab = PictureList[nummurs].Split('/');
        if (MapesNr < 10)
        {
            string serchBiggPic = "0" + Convert.ToString(MapesNr) + TextGab[5].Substring(5);

            Response.Redirect("GaleryZoom.aspx?value=" + serchBiggPic);
        }
        else
        {
            string serchBiggPic = Convert.ToString(MapesNr) + TextGab[5].Substring(5);

            Response.Redirect("GaleryZoom.aspx?value=" + serchBiggPic);
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //int Nr = e.Item.ItemIndex;
        //ImageButton img = ((ImageButton)e.Item.FindControl("ImageButton1"));
        //img.PostBackUrl =  Galrow[Nr].ItemArray[2].ToString();
    }
}
